package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.entity.MyArray;
import com.example.demo.entity.Prize;
import com.example.demo.entity.PrizesInActivities;
import com.example.demo.mapper.PrizeMapper;
import com.example.demo.service.PrizeService;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class PrizeServiceImpl implements PrizeService {
    @Resource
    private PrizeMapper prizeMapper;

    @Override
    public List<PrizesInActivities> getPrizeByActivityId(Integer activityId){
        List<PrizesInActivities> list=prizeMapper.getPrizeByActivityId(activityId);
        return list;
    };

    @Override
    public Integer addPrize(Integer activityId, Integer goodId, Float goodProbability){
        Integer businessId = prizeMapper.getBusinessIdByGoodId(goodId);
        Prize prize=new Prize(activityId, businessId, goodId, goodProbability);
        try{
            return prizeMapper.insert(prize);
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public Integer editProbability(Integer activityId, Integer goodId, Float goodProbability){
        Integer businessId = prizeMapper.getBusinessIdByGoodId(goodId);
        UpdateWrapper<Prize> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("activity_id",activityId);
        updateWrapper.eq("business_id",businessId);
        updateWrapper.eq("good_id",goodId);
        Prize prize = new Prize(activityId, businessId, goodId, goodProbability);
        try {
            return prizeMapper.update(prize,updateWrapper);
        }catch (Exception e){
            return 0;
        }

    }

    public Integer lotteryDraw(Integer activityId){
        QueryWrapper<Prize> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id",activityId);
        List<Prize> res = prizeMapper.selectList(queryWrapper);
        float pro = 0.0f;
        for(Prize p : res){
            pro += p.getGoodProbability();
        }
        res.add(new Prize(null,null,-1,1 - pro));
        Integer result = null;
        try {
            float sum = 0.0f;//概率数组的总概率精度
            float min = 0.0f;//
            for(Prize prize : res){
                BigDecimal beforeSum = new BigDecimal(Float.toString(sum));
                BigDecimal objValue = new BigDecimal(Float.toString(prize.getGoodProbability()));
                sum = beforeSum.add(objValue).floatValue();
            }

            for(Prize prize : res){//概率数组循环
                BigDecimal db = new BigDecimal(Math.random() * (sum - min) + min);
                BigDecimal b = new BigDecimal(Float.toString(prize.getGoodProbability()));
                if(db.compareTo(b) == -1){//中奖
                    result = prize.getGoodId();
                    break;
                }else{
                    sum -= prize.getGoodProbability();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int delPrize(MyArray prizes) {
        try {
            for (Prize prize : prizes.getPrizes()) {
                QueryWrapper<Prize> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("good_id", prize.getGoodId());
                queryWrapper.eq("activity_id", prize.getActivityId());
                prizeMapper.delete(queryWrapper);
            }
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

}
